package com.starryx.sim.mvc.net.handler.impl;


import com.starryx.sim.mvc.net.handler.AbstractRequestHandler;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.codec.Charsets;

import java.util.Arrays;


public class PostMethodHandler extends AbstractRequestHandler {

	@Override
	protected String getParam(FullHttpRequest fullHttpRequest) {
		HttpHeaders headers = fullHttpRequest.headers();
		boolean isJsonContext = Arrays.asList(headers.get("Content-Type").split(";")).contains("application/json");
		if (!isJsonContext) {
			throw new IllegalArgumentException("POST 请求只能处理 application/json 类型的数据");
		}
		return fullHttpRequest.content().toString(Charsets.toCharset(CharEncoding.UTF_8));
	}

}
